package git.soulbgm.security.pojo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import git.soulbgm.pojo.entity.User;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

/**
 * 自定义登录用户身份权限
 *
 * @author SoulBGM
 * @date 2020-07-30
 */
public class LoginUser implements UserDetails {

    private Long id;

    private String nickname;

    private String username;

    private String password;

    /**
     * 用户唯一标识
     */
    private String token;

    /**
     * 过期时间
     */
    private Long expireTime;

    /**
     * 用户拥有的权限
     */
    private Set<String> permissions;

    /**
     * 用户登录时间
     */
    private Long loginTime;

    /**
     * 登录IP地址
     */
    private String loginIpAddress;

    public LoginUser() {
    }

    public LoginUser(Long id, String nickname, String username, String password, Set<String> permissions, String loginIpAddress) {
        this.id = id;
        this.nickname = nickname;
        this.username = username;
        this.password = password;
        this.permissions = permissions;
        this.loginTime = System.currentTimeMillis();
        this.loginIpAddress = loginIpAddress;
    }

    public LoginUser(User user, Set<String> permissions) {
        this.id = user.getId();
        this.nickname = user.getNickname();
        this.username = user.getUsername();
        this.password = user.getPassword();
        this.permissions = permissions;
        this.loginTime = System.currentTimeMillis();
    }

    public LoginUser(User user, Set<String> permissions, String loginIpAddress) {
        this(user, permissions);
        this.loginIpAddress = loginIpAddress;
    }

    public Long getId() {
        return id;
    }

    public String getNickname() {
        return nickname;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public Long getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(Long expireTime) {
        this.expireTime = expireTime;
    }

    public Set<String> getPermissions() {
        return permissions;
    }

    public Long getLoginTime() {
        return loginTime;
    }

    public void setLoginTime(Long loginTime) {
        this.loginTime = loginTime;
    }

    public String getLoginIpAddress() {
        return loginIpAddress;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setPermissions(Set<String> permissions) {
        this.permissions = permissions;
    }

    public void setLoginIpAddress(String loginIpAddress) {
        this.loginIpAddress = loginIpAddress;
    }

    /**
     * 获得权限列表
     */
    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @JsonIgnore
    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    /**
     * 账户是否未过期,过期无法验证
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 指定用户是否解锁,锁定的用户无法进行身份验证
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 是否已过期的用户的凭据(密码),过期的凭据防止认证
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否可用 ,禁用的用户不能身份验证
     */
    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public boolean equals(Object o) {
        // 自定义equals方法
        boolean isEqual = false;
        if (o != null && LoginUser.class.isAssignableFrom(o.getClass())) {
            LoginUser userInfo = (LoginUser) o;
            isEqual = new EqualsBuilder()
                    .append(getId(), userInfo.getId())
                    .append(getUsername(), userInfo.getUsername())
                    .isEquals();
        }
        return isEqual;
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(getId())
                .append(getUsername()).toHashCode();
    }

    @Override
    public String toString() {
        return username;
    }
}
